<?php
  require_once '../../Helpers/DbHelper.php';
  require_once '../../Controllers/AdminController.php';
  require_once '../../Controllers/SchoolController.php';
  require_once '../../Controllers/UserController.php';
  require_once '../../Helpers/PathHelper.php';

  $u = new UserController();
  $u->evaluatePagePermission(UserController::ADMINISTRATOR);
  //show default list of payment logs, if any
  $a = new AdminController();
  $paymentLogs = $a->getPaymentLogs();

  if(isset($_POST['add-payment']) && !empty($_POST))
  {
    if(!empty($_POST['student-id']) && !empty($_POST['payment-amount']) && !empty($_POST['receipt-number']) && !empty($_POST['payment-date']))
    {
      $studentId = $_POST['student-id'];
      $paymentAmount = $_POST['payment-amount'];
      $receiptNumber = $_POST['receipt-number'];
      $paymentDate = $_POST['payment-date'];

      $a->addPaymentLog($studentId,$paymentAmount,$receiptNumber,$paymentDate);
      $paymentLogs = $a->getPaymentLogs();
    }
  }
  else if(isset($_GET['action']) && $_POST['delete-payments'])
  {
    //delete selected payment logs.
    $ids = $_POST['paymentId'];
    //
    for($i=0;$i<count($ids);$i++)
      $a->removePaymentLogs($ids[$i]);
    //show updated payment logs table.
    $paymentLogs = $a->getPaymentLogs();
  }
?>
<html>
  <head>
    <title>School Enrollment :: Payments</title>
    <?php
      require_once '../commons.php';
      renderHeaderStyles() ;
    ?>
    <link href="../../assets/jquery_ui.css" rel="stylesheet" type="text/css" />
    <link href="../../assets/theme.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../../assets/js/jquery-1.6.2.min.js"></script>
     <script type="text/javascript" src="../../assets/js/jquery-ui-1.8.16.custom.min.js"></script>
     <script type="text/javascript" src="../../assets/js/jquery.ui.tabs.js"></script>
    <script type="text/javascript">
      $(document).ready(function()
      {
        $("#datepicker").datepicker({dateFormat:'yy-mm-dd'});
      }

      );
    </script>
  </head>
  <body>
    <div class="admin-container">
      <?php renderAdminNav(); ?>
    </div>
  <div class="container">
    <h1>Add Payment Logs</h1>
    <form name="fees-form" method="post">
      <table align="center" class="labels">
        <tr>
          <td><label for="student-id">Student ID:</label></td>
          <td><input type="text" name="student-id" /></td>
        </tr>
        <tr>
          <td><label for="payment-amount">Payment Amount:</label></td>
          <td><input type="text" name="payment-amount" /></td>
        </tr>
        <tr>
          <td><label for="receipt-number">Receipt Number:</label></td>
          <td><input type="text" name="receipt-number" /></td>
        </tr>
        <tr>
          <td><label for="payment-date">Payment Date:</label></td>
          <td><input type="text" name="payment-date" id="datepicker" /></td>
        </tr>
        <tr><td><input type="submit" name="add-payment" value="Add Payment" /></td></tr>
      </table>
    </form>
    <hr />
    <div class="overflow">
    <table cellspacing="5" align="center" class="table">
      <form name="fees-list" method="post" action="payments.php?action=delete">
      <tr>
        <th>&nbsp;</th>
        <th width="150">Student ID</th>
        <th>Payment Amount</th>
        <th>Payment Date</th>
        <th>Receipt Number</th>
      </tr>
      <?php
        if(mysql_num_rows($paymentLogs) < 1)
          echo "<tr><td colspan='3'>No data available.</td></tr>";
        while($rows = mysql_fetch_assoc($paymentLogs))
        {
          $paymentId = $rows['PaymentId'];
          $studentId = $rows['StudentId'];
          $paymentAmount = $rows['PaymentAmount'];
          $paymentDate = $rows['PaymentDate'];
          $receiptNumber = $rows['ReceiptNumber'];


          echo "<tr>
            <td><input type='checkbox' name='paymentId[]' value='$paymentId' /></td>
            <td>$studentId</td>
            <td>$paymentAmount</td>
            <td>$paymentDate</td>
            <td>$receiptNumber</td>
            <td><a href='edit_payment.php?pid=$paymentId'>Edit</a></td>
          </tr>";
        }
        echo "<tr><td><input type='submit' name='delete-payments' value='Delete Selected' />";
        echo "</tr>";
      ?>
    </table>
    </div>
  </div>
  </body>
</html>